package safari.controller.message;

import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import safari.model.common.ListVO;
import safari.model.member.MemberVO;
import safari.model.message.IdAndPageVO;
import safari.model.message.MessageService;
import safari.model.message.MessageVO;

public class MessageController extends MultiActionController{
	private MessageService ms;
	public MessageController(MessageService ms){
		this.ms=ms;
	}
	
	public ModelAndView newMessage(HttpServletRequest request, HttpServletResponse response,HttpSession session,MessageVO msgvo) throws SQLException{
		//login 안하고 주소에 치고 들어왔을 경우
		String path="/message/newMessage";
		ModelAndView resultMV=new ModelAndView(path);
		//login 했을 때
				if(session.getAttribute("userInfo")!=null){
					MemberVO mvo=(MemberVO) session.getAttribute("userInfo");
					String senderId=mvo.getId();
					msgvo.setSenderId(senderId);
					// -1: 받는사람 id 없음, 0: 쪽지보내기 실패, 1: 성공
					int result=ms.newMessage(msgvo);
					resultMV=new ModelAndView("JsonView","result",result);
				}
		
		return resultMV;
	}
	
	public String goNewMessage(HttpServletRequest request, HttpServletResponse response,HttpSession session,MessageVO msgvo){
		return "newMessage";
	}
	
	public ModelAndView messageBox(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws SQLException{
		MemberVO mvo=(MemberVO) session.getAttribute("userInfo");
		IdAndPageVO ipvo=new IdAndPageVO(mvo.getId(),Integer.parseInt(request.getParameter("pageRe")));
		ListVO receivedList=ms.getReceivedMessageList(ipvo);
		HashMap map=new HashMap();
		map.put("receivedList", receivedList);
		ipvo.setPage(Integer.parseInt(request.getParameter("pageSd")));
		ListVO sendedList=ms.getSendedMessageList(ipvo);
		map.put("sendedList", sendedList);
		return new ModelAndView("messageBox","info",map);
	}
	
	/**
	 * ajax로 결과 다시 보내주는 메서드
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 * @throws SQLException
	 */
/*	public ModelAndView messageBox2(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws SQLException{
		MemberVO mvo=(MemberVO) session.getAttribute("userInfo");
		IdAndPageVO ipvo=new IdAndPageVO(mvo.getId(),Integer.parseInt(request.getParameter("pageRe")));
		ListVO receivedList=ms.getReceivedMessageList(ipvo);
		HashMap map=new HashMap();
		map.put("receivedList", receivedList);
		ipvo.setPage(Integer.parseInt(request.getParameter("pageSd")));
		ListVO sendedList=ms.getSendedMessageList(ipvo);
		map.put("sendedList", sendedList);
		
		return new ModelAndView("JsonView","info",map);
	}*/
	
	public ModelAndView viewOneMessage(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int no=Integer.parseInt(request.getParameter("no"));
		MessageVO msgvo=ms.viewOneMessage(no);
		return new ModelAndView("message/viewOneMessage","result",msgvo);
	}
	
	public ModelAndView deleteMessage(HttpServletRequest request, HttpServletResponse response) throws NumberFormatException, SQLException{
		int result=ms.deleteMessage(Integer.parseInt(request.getParameter("no")));
		String path="error";
		//delete 성공하면
		if(result!=0){
			path="JsonView";
		}
		return new ModelAndView(path,"result",result);
	}
}
